package pingan.com.imandtwo.g_mvp.presenter;

import java.util.List;
import java.util.Map;

import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import io.reactivex.subscribers.DisposableSubscriber;
import pingan.com.imandtwo.bean.Shouye;
import pingan.com.imandtwo.g_mvp.model.IMModel;
import pingan.com.imandtwo.g_mvp.model.Model;
import pingan.com.imandtwo.g_mvp.view.IView;

/**
 * Created by 迷人的脚毛！！ on 2018/2/6.
 */

public class Presenter implements IMPresenter {

    private IView iv;
    private DisposableSubscriber<Shouye> disposableSubscriber;
    public void attachView(IView iv) {
        this.iv = iv;
    }
    public void delete() {
        if (iv != null) {
            iv = null;
        }
        if (disposableSubscriber != null) {
            if (disposableSubscriber.isDisposed()) {
                disposableSubscriber.dispose();
            }
        }
    }

    @Override
    public void getData(String url, Map<String, String> map) {
        //首页
        IMModel model=new Model(this);
        model.getData(url,map);
    }

    public void getShouye(Flowable<Shouye> shouye){
        disposableSubscriber = shouye.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new DisposableSubscriber<Shouye>() {
                    @Override
                    public void onNext(Shouye shouye) {

                        if (shouye!=null){
                            List<Shouye.NewslistBean> newslist = shouye.getNewslist();
                         iv.success(newslist);
                        }
                    }

                    @Override
                    public void onError(Throwable t) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
